import os
from datetime import datetime
from storage_manager import load_reservations_from_file, save_reservations_to_file, load_layout_config_from_file
from seat_manager import view_seats_interaction
from reservation_handler import (
    make_reservation_interaction,
    cancel_reservation_interaction,
    view_my_reservations_interaction
)
from admin_functions import admin_menu_interaction


def display_main_menu():
    print("\n=== 自习室预约系统 ===")
    print("1. 查看座位状态")
    print("2. 预约座位")
    print("3. 取消预约")
    print("4. 查看我的预约")
    print("5. 管理员功能")
    print("6. 退出程序")
def main_loop():
    # 加载数据
    all_reservations = load_reservations_from_file()
    layout_info = load_layout_config_from_file()

    while True:
        display_main_menu()
        choice = input("请输入您的选择 (1-6): ")

        if choice == "1":
            view_seats_interaction(all_reservations, layout_info)
        elif choice == "2":
            new_reservation = make_reservation_interaction(all_reservations, layout_info)
            if new_reservation:
                all_reservations.append(new_reservation)
                save_reservations_to_file(all_reservations)
        elif choice == "3":
            all_reservations = cancel_reservation_interaction(all_reservations, layout_info)
            if all_reservations is not None:
                save_reservations_to_file(all_reservations)
        elif choice == "4":
            view_my_reservations_interaction(all_reservations)
        elif choice == "5":
            # 调用管理员功能时传递 layout_info，并接收更新后的数据
            updated_reservations, updated_layout = admin_menu_interaction(all_reservations, layout_info)
            if updated_reservations is not None:
                all_reservations = updated_reservations
                layout_info = updated_layout  # 保存更新后的座位布局
                save_reservations_to_file(all_reservations)
        elif choice == "6":
            print("感谢使用自习室预约系统，再见！")
            break
        else:
            print("无效的选择，请重新输入。")

if __name__ == "__main__":
    main_loop()